use apperror::{AnyError, AnyErrorExt};
use rmpredictdb::model::User;
use tauri_plugin_store::StoreExt;

use crate::{extensions_trait::InnerUser, func::ValueExt};

pub mod predict;
pub mod user;
#[tauri::command]
pub fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

pub fn initialization_load(app: &tauri::AppHandle) -> Result<User, ()> {
    let store = app.store("user.json").unwrap();
    let user = store.get("user");
    if let Some(mut user) = user {
        let user = user.take().convert(InnerUser::default(), User::default());
        let _ = user.is_exist().map_err(|e| {
            log::error!("{}", AnyError::establish(e).report());
        })?;
        log::info!("{:#?}", user);
        Ok(user)
    } else {
        Err(())
    }
}
